//
//  TimeWarning.m
//  Renyiting
//
//  Created by timmy on 16/9/6.
//  Copyright © 2016年 timmy. All rights reserved.
//

#import "TimeWarning.h"

#define CLC_WIDTH  VIEW_WIDTH(rect) / 4 * 3
#define CLC_DESTANCE   SAFE_DESTANCE
#define pi 3.14159265359
#define   DEGREES_TO_RADIANS(degrees)  ((pi * degrees)/ 180)
#define DEGRESS    175

@interface TimeWarning ()

@property(nonatomic,strong)UIColor *foregroundColor;

@end

@implementation TimeWarning

- (instancetype)initWithFrame:(CGRect)frame backgroundColor:(UIColor *)backColor foregroundColor:(UIColor *)foregroundColor {

    self = [super initWithFrame:frame];
    if (self) {
        self.foregroundColor = foregroundColor;
        [self layoutSubViewsWithFrame:frame backgroundColor:backColor foregroundColor:foregroundColor];
    }
    return self;

}

- (void)layoutSubViewsWithFrame:(CGRect)frame backgroundColor:(UIColor *)backColor foregroundColor:(UIColor *)foregroundColor{

    self.backgroundColor = backColor;

}

- (void)drawRect:(CGRect)rect {

    CGContextRef bigContext = UIGraphicsGetCurrentContext();
    CGRect aRect= CGRectMake((VIEW_WIDTH(rect) - CLC_WIDTH) / 2, VIEW_HEIGHT(rect) / 5, CLC_WIDTH, CLC_WIDTH);
    CGContextSetRGBStrokeColor(bigContext, [self.foregroundColor red], [self.foregroundColor green], [self.foregroundColor blue], [self.foregroundColor alpha]);
    CGContextSetLineWidth(bigContext, 1);
    CGContextAddEllipseInRect(bigContext, aRect);
    CGContextDrawPath(bigContext, kCGPathStroke);
    
    CGContextRef smallContext = UIGraphicsGetCurrentContext();
    CGRect bRect = CGRectMake(VIEW_MIN_X(aRect) + CLC_DESTANCE, VIEW_MIN_Y(aRect) + CLC_DESTANCE, VIEW_WIDTH(aRect) - 2 * CLC_DESTANCE, VIEW_HEIGHT(aRect) - 2 * CLC_DESTANCE);
    CGContextSetRGBStrokeColor(smallContext, [self.foregroundColor red], [self.foregroundColor green], [self.foregroundColor blue],  [self.foregroundColor alpha]);
    CGContextSetLineWidth(smallContext, 1);
    CGContextAddEllipseInRect(smallContext, bRect);
    CGContextDrawPath(smallContext, kCGPathStroke);
    
    CGContextRef textContext = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(textContext, 1.0);
    CGContextSetRGBFillColor (textContext, [self.foregroundColor red], [self.foregroundColor green], [self.foregroundColor blue],  [self.foregroundColor alpha]);
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    NSDictionary *attributes = @{ NSParagraphStyleAttributeName: paragraphStyle, NSForegroundColorAttributeName:COMMON_COLOR, NSFontAttributeName :PO_50 };
    [@"01:05:59" drawInRect:CGRectMake(VIEW_MIN_X(bRect), CGRectGetMidY(bRect) - 50, VIEW_WIDTH(bRect),100) withAttributes:attributes];
    
    /*
     参数：
     center:弧线中心点的坐标
     radius:弧线所在圆的半径
     startAngle:弧线开始的角度值
     endAngle:弧线结束的角度值
     clockwise:是否顺时针画弧线
     
     */
    UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetMidX(bRect), CGRectGetMidY(bRect)) radius:VIEW_WIDTH(bRect) / 2 startAngle:DEGREES_TO_RADIANS(90 - DEGRESS) endAngle:DEGREES_TO_RADIANS(90 + DEGRESS) clockwise:YES];
    aPath.lineWidth = 1.0;
    aPath.lineCapStyle = kCGLineCapRound; //线条拐角
    aPath.lineJoinStyle = kCGLineCapRound; //终点处理
    [aPath fill];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, VIEW_MAX_Y(bRect) - 50, SCREEN_WIDTH, 50)];
    [self addSubview:label];
    label.textAlignment = NSTextAlignmentCenter;
    [label text:@"结束停车" textColor:[UIColor whiteColor] font:PO_15 attributeText:nil];
    [self createAnimation: - M_PI / 2 andEndAngle:M_PI * 2 x:CGRectGetMidX(bRect) y:CGRectGetMidY(bRect) radius:VIEW_WIDTH(bRect) / 2];
    label.userInteractionEnabled = YES;
    UITapGestureRecognizer *stop = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(stopAction:)];
    [label addGestureRecognizer:stop];
    
}

-(void)createAnimation:(float)startAngle andEndAngle:(float)endAngle x:(CGFloat)x y:(CGFloat)y radius:(CGFloat)radius {
    
    //创建运转动画
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.duration = 15.0;
    pathAnimation.repeatCount = 1000;
    //设置运转动画的路径
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGPathAddArc(curvedPath, NULL, x, y, radius, startAngle, endAngle, 0);
    pathAnimation.path = curvedPath;
    CGPathRelease(curvedPath);

    self.littlePic  = [[UIImageView alloc] init];
    [self addSubview:self.littlePic ];
    self.littlePic .frame = CGRectMake(x, y, 5, 5);
    self.littlePic .backgroundColor = COMMON_COLOR;
    self.littlePic.layer.cornerRadius = 2.5;
    self.littlePic.clipsToBounds = YES;
    //设置运转的动画
    [self.littlePic.layer addAnimation:pathAnimation forKey:@"moveTheCircleOne"];
}

- (void)stopAction:(UITapGestureRecognizer *)sender {

    [self.delegate stopParking];

}

@end
